//
//  WNHomeViewController.swift
//  WNWeibo
//
//  Created by 风不会停息 on 2017/7/22.
//  Copyright © 2017年 风不会停息. All rights reserved.
//

import UIKit
import YYModel

class WNHomeViewController: WNVistorViewController {
    
//    lazy var statusList : [WNStatus] = [WNStatus]()
    fileprivate lazy var homeViewModel : WNHomeViewModel = WNHomeViewModel()
    
    //上拉风火轮
    fileprivate lazy var pullUpView : UIActivityIndicatorView = {
        let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        
        indicatorView.color = UIColor.purple
        
        return indicatorView
    }()
    
    // 下拉刷新控件
//    fileprivate lazy var pullDownView: UIRefreshControl = {
//        let refreshCtr = UIRefreshControl()
//        //  添加事件
//        refreshCtr.addTarget(self, action: #selector(pullDownRefreshing), for: .valueChanged)
//        return refreshCtr
//    }()
    
    //自定义下拉控件
    fileprivate lazy var pullDownView : WNRefreshiControl = {
        let refreshCtr = WNRefreshiControl()
        
        refreshCtr.addTarget(self, action: #selector(pullDownRefreshing), for: .valueChanged)
        
        return refreshCtr
        
    }()
    
    //tipLabel
    fileprivate lazy var  tipLabel : UILabel = {
        let label = UILabel(fontSize: 12, textColor: UIColor.white)
        label.backgroundColor = UIColor.orange
        label.text = "没有加载到最新微博"
        label.textAlignment = .center
        label.isHidden = true
        return label
    }()
    
    @objc private func pullDownRefreshing() {
        print("下拉刷新")
        loadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        if !isLogin {
            //  没有登录修改访客视图信息
            visitorView?.updateVisitorInfo(message: nil, imageName: nil)
        }else
        {
            
            loadData()
            
            setupTableView()
            
            setupTipLable()
        }
 
    }
    
    //tipLabel设置方法
    private func setupTipLable(){
        
        if let nav = self.navigationController{
            nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
            
            tipLabel.frame = CGRect(x: 0, y: nav.navigationBar.frame.maxY-35, width: nav.navigationBar.width, height: 35)
        }
    }
    
    //tipLabel 动画开起
    private func startTipLabelAnimation(message : String){
        
        if tipLabel.isHidden == false {
            
            return
        }
        
        tipLabel.isHidden = false
        
        tipLabel.text = message
        
        UIView.animate(withDuration: 1, animations: {
            
            self.tipLabel.transform = CGAffineTransform(translationX: 0, y: self.tipLabel.height)
            
        }) { (_) in
            
            UIView.animate(withDuration: 1, animations: {
                self.tipLabel.transform = CGAffineTransform.identity
            }, completion: { (_) in
                self.tipLabel.isHidden = true
            })
        }
    }
    
    //tableview相关操作
    private func setupTableView(){
        
        tableView.register(WNHomeTableViewCell.self, forCellReuseIdentifier: HomeCellIdentifier)
        
        tableView.rowHeight = UITableViewAutomaticDimension
        
        tableView.estimatedRowHeight = 200
        
        //分割线去掉
        tableView.separatorStyle = .none
        
        tableView.tableFooterView = pullUpView
        
        tableView.addSubview(pullDownView)
        
    }
    
    //  加载微博数据
    func loadData() {
        
        homeViewModel.loadData(isUp: pullUpView.isAnimating) { (isSuccess , message ) in

            if self.pullUpView.isAnimating == false{
                self.startTipLabelAnimation(message: message)
            }
            
            if isSuccess{
                
                self.endRefreshing()
                self.tableView.reloadData()
            }
        }
    }

    //结束刷新
    private func endRefreshing(){
        
        pullUpView.stopAnimating()
        
        pullDownView.endRefreshing()
    }
    
}
extension WNHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return homeViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: HomeCellIdentifier, for: indexPath) as! WNHomeTableViewCell
        //  获取模型
        let statusViewModel = homeViewModel.statusList[indexPath.row]
        
        cell.statusViewModel = statusViewModel
        
        cell.selectionStyle = .none
        
        return cell
    }
    
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        if indexPath.row == homeViewModel.statusList.count - 1 && pullUpView.isAnimating == false {
//            print("滚动到最后一个cell")
            //  开启风火轮
            pullUpView.startAnimating()
            //  微博数据
            loadData()
        }
    }
    
    
    
    
    
    
    
    
    
    
    

}
